import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Scanner;

public class Algorithm {
	/*constants*/
	public static void main(String[] args) {
		Scanner sc= new Scanner(System.in);
		System.out.print("Enter the size of the group: ");
		int sizeOfGroup=sc.nextInt();
		System.out.print("Enter value of tau: ");
		float tau =sc.nextFloat();
		System.out.print("Enter value of delta: ");
		float delta=sc.nextFloat();
		System.out.print("Enter the infinity-norm of the function: ");
		float infNorm=sc.nextFloat();
		System.out.print("Enter 1 for 'on-the-fly', 2 for XML input: ");
		Function funct;
		if(sc.nextInt()==1){
			funct=new flyFunc();
		}else{
			System.out.print("Enter XML filename: ");
			String a=sc.next();
			funct=new XMLFunc(sizeOfGroup, a);
		}
		while(true){
			List<subSet> list=SFT(sizeOfGroup, tau, delta, infNorm, funct);
			System.out.println("The dominant coefficients are: ");
			for(subSet p:list){
				p.print();
				System.out.print(", ");
			}
			System.out.println();
			System.out.print("Enter 1 to quit, 2 to continue: ");
			if(sc.nextInt()==1){
				System.out.println("Goodbye!");
				return;
			}
			System.out.print("Enter 1 to change size of group, 2 to resume: ");
			if(sc.nextInt()==1){
				System.out.print("Enter the size of the group: ");
				sizeOfGroup=sc.nextInt();
			}
			System.out.print("Enter 1 to change tau, 2 to resume: ");
			if(sc.nextInt()==1){
				System.out.print("Enter tau: ");
				tau=sc.nextFloat();
			}
			System.out.print("Enter 1 to change delta, 2 to resume: ");
			if(sc.nextInt()==1){
				System.out.print("Enter delta: ");
				delta=sc.nextFloat();
			}
			System.out.print("Enter 1 to change infinity-norm, 2 to resume: ");
			if(sc.nextInt()==1){
				System.out.print("Enter infinity-norm: ");
				infNorm=sc.nextFloat();
			}
			System.out.print("Enter 1 to change source of function, 2 to resume: ");
			if(sc.nextInt()==1){
				System.out.print("Enter 1 for 'on-the-fly', 2 for XML input: ");
				if(sc.nextInt()==1){
					funct=new flyFunc();
				}else{
					System.out.print("Enter XML filename: ");
					funct=new XMLFunc(sizeOfGroup, sc.nextLine());
				}
			}

		}
	}

	private static List<subSet> SFT(int sizeOfGroup, float tau, float delta, float infNorm, Function funct) {
		List<subSet> list;
		Map<Integer, complex> query;
		float delta_mark=(float) (delta/(Math.pow(funct.eucNorm(sizeOfGroup)/tau,1.5)*(Math.log(sizeOfGroup)/Math.log(2))));
		list=GenerateQueries(sizeOfGroup, tau/36, infNorm, delta_mark);
		query=Query(sizeOfGroup, list, funct);
		return FixedQueries(sizeOfGroup, tau, list, query);	
	}

	private static Map<Integer, complex> Query(int sizeOfGroup, List<subSet> list, Function funct) {
		subSet q=new subSet();
		Iterator<Integer> iter =list.get(0).iter();
		while(iter.hasNext()){
			int i=iter.next();
			for (subSet s : list) {
				if(s!=list.get(0)){
					Iterator<Integer> iter2=s.iter();
					while(iter2.hasNext()){
						int j=iter2.next();
						int a=(i-j)%sizeOfGroup;
						if(a<0)
							a+=sizeOfGroup;
						q.add_n_keep(a);
					}
				}
			}
		}		
		Map<Integer, complex> query=new HashMap<Integer, complex>();
		iter=q.iter();
		while(iter.hasNext()){
			int i=iter.next();
			query.put(i, funct.function(i));
		}
		return query;
	}

	private static List<subSet> FixedQueries(int sizeOfGroup, float tau, List<subSet> list, Map<Integer, complex> query) {
		List<subSet> curr_candidate=new ArrayList<subSet>();
		List<subSet> next_candidate=new ArrayList<subSet>();
		subSet group=new subSet();
		group.add_n_keep(0);
		group.add_n_keep(sizeOfGroup-1);
		curr_candidate.add(group);
		for (int l = 1; l < Math.log(sizeOfGroup)/Math.log(2)+1; l++) {
			for (subSet sset : curr_candidate) {
				int a_tag=sset.getLow();
				int b_tag=sset.getHigh();
				int a=a_tag;
				int b=(a_tag+b_tag)/2;
				for(int i=0; i<2; i++){
					subSet sset2=new subSet();
					sset2.add_n_keep(a);
					sset2.add_n_keep(b);
					if(Distinguish(sizeOfGroup, sset2, tau, list.get(0), list.get(l), query)==1){
						next_candidate.add(sset2);
					}
					a=b+1;
					b=b_tag;
				}
			}
			curr_candidate=next_candidate;
			next_candidate=new ArrayList<subSet>();
		}
		for (subSet subSet : curr_candidate) {
			if(subSet.size()==1){
				next_candidate.add(subSet);	
			}
		}
		return next_candidate;
	}

	private static int Distinguish(int sizeOfGroup, subSet sset, float tau, subSet A, subSet B, Map<Integer, complex> query) {
		double est=0;
		int a=sset.getLow();
		int b=sset.getHigh();
		double v = Math.floor((a+b)/2);
		Iterator<Integer> iter=A.iter();
		while(iter.hasNext()){
			int i=iter.next();
			complex sum = new complex(0,0);
			Iterator<Integer> iter2=B.iter();
			while(iter2.hasNext()){
				int j= iter2.next();
				complex chiValue = chi(sizeOfGroup,v,j);
				int number=(i-j)%sizeOfGroup;
				if(number<0)
					number+=sizeOfGroup;
				sum.addComplex(complex.divComplex(complex.mulComplex(chiValue,query.get(number)),(double)B.size()));
			}
			est += (Math.pow(sum.norm(), 2)/(double)A.size());
		}

		
		if(tau*5/36<=est)
			return 1;
		return 0;
	}
	
	static complex chi(int sizeOfGroup, double v, int j) {
		double arg = 2 * Math.PI * (((double)j)/((double)sizeOfGroup)) * v;
		double re = Math.cos(arg);
		double im = Math.sin(arg);

		
		return new complex(re, im);
	}

	private static List<subSet> GenerateQueries(int sizeOfGroup, float gamma, float infNorm, float delta) {
		double eta = Math.min(Math.min(gamma, Math.sqrt(gamma)),(gamma/infNorm));
		double tmpCoeff = Math.pow(infNorm/eta, 2);
		long mA = (long) ((0.00002) * Math.ceil(tmpCoeff*Math.log(1.0/delta))/Math.log(2));
		long mB = (long) ((0.0002) * Math.ceil(tmpCoeff*Math.log(infNorm/(delta*gamma))/Math.log(2)));
		List<subSet> list=new ArrayList<subSet>();
		list.add(createSubgroup(Math.min(mA, sizeOfGroup), sizeOfGroup));
		for (int l = 0; l < Math.log(sizeOfGroup)/Math.log(2); l++) {
			list.add(createSubgroup(Math.min(Math.min(mB, Math.pow(2, l-1)), sizeOfGroup), (int) Math.min(Math.round(Math.pow(2, l-1)), sizeOfGroup)));
		}
		return list;
	}

	private static subSet createSubgroup(double min, int sizeOfGroup) {
		Random rand=new Random();
		subSet pair=new subSet();
		while(pair.size()<min){
			pair.add_n_keep(rand.nextInt(sizeOfGroup));
		}
		return pair;
	}
}
